/**
 * Created by Administrator on 2017/7/11.
 */
$(function () {
    $("#jqGrid").jqGrid({
        url: 'sharepic/list',
        datatype: "json",
        colModel: [
            { label: 'sid', name: 'sid', index: 'sid', width: 80, key: true ,hidden:true},
            { label: '标题', name: 'shareName', index: 'shareName', width: 80 },
            { label: '描述', name: 'shareDesc', index: 'shareDesc', width: 80 },
            { label: '图片', name: 'fileUrl', index: 'fileUrl', width: 80}
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "result.list",
            page: "result.currPage",
            total: "result.totalPage",
            records: "result.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });

    $("#photoImage").click(function () {
        $("#shareImgFile").click();
    });

    $("#shareImgFile").change(function () {
        var objUrl = getObjectURL(this.files[0]); //获取图片的路径，该路径不是图片在本地的路径
        if (objUrl != null) {
            $("#photoImage").attr("src", objUrl);
            vm.sharepic.image = objUrl;
        }
    });
});

var vm = new Vue({
    el: "#rrapp",
    data:{
        showList:true,
        showAdd:false,
        title:"",
        sharepic:{},
        apps:[]
    },
    methods:{
        loadApps:function () {
            $.ajax({
                url:"/platform/oem/app/list?page=1&limit=1000",
                type:"POST",
                dataType:"JSON",
                success:function (result) {
                    if (result.code == 200){
                        vm.apps = result.data.list;
                    }else
                        alert(result.msg);
                },
                error:function (e) {
                    console.log("error",e);
                    alert("请求失败，请重试")
                }
            })
        },
        add:function () {
            vm.showAdd = true;
            vm.showList = false;
            vm.title = "添加分享图片信息";
            vm.sharepic = {};
        },
        del:function () {
            var sid = getSelectedRow();
            window.confirm("确定要删除么？此操作不可恢复",function (s) {
                $.ajax({
                    url:"/platform/oem/sharepic/delete/"+sid,
                    type:"GET",
                    dataType:"JSON",
                    success:function (result) {
                        if (result.code == 200){
                            vm.reload();
                            alert("操作成功");
                        }else
                            alert(result.msg);
                    },
                    error:function (e) {
                        console.log("error",e);
                        alert("请求失败，请重试");
                    }
                });
            });
        },
        update:function () {
            vm.showAdd = true;
            vm.showList = false;
            vm.title = "修改banner信息";
            vm.sharepic = $("#jqGrid").jqGrid().getRowData(getSelectedRow());
        },
        saveOrUpdate:function () {
            Vue.delete(this.sharepic,'fileUrl');
            var urlStr = this.sharepic.sid == null ? "/platform/oem/sharepic/save" : "/platform/oem/sharepic/update";
            $.ajaxFileUpload({
                url: urlStr,
                data: JSON.parse(JSON.stringify(vm.sharepic)),
                type: "POST",
                contentType: "multipart/form-data",
                secureuri: false,
                fileElementId: 'shareImgFile',
                dataType: 'json',
                success: function (result) {
                    if (result.code == 200) {
                        vm.reload();
                        alert("操作成功");
                    } else {
                        alert(result.msg);
                        if (this.lecture.id == null) {
                            $("#photoImage").attr("src", "");
                        }
                    }
                },
                complete: function () {
                    $("#shareImgFile").replaceWith('<input type="file" style="display: none;" ref="inputFile" accept="image/*" name="shareImgFile" id="shareImgFile"class="form-control" placeholder="file"/>');
                    $("#shareImgFile").change(function () {
                        var objUrl = getObjectURL(this.files[0]); //获取图片的路径，该路径不是图片在本地的路径
                        if (objUrl != null) {
                            $("#photoImage").attr("src", objUrl);
                            vm.sharepic.image = objUrl;
                        }
                    });
                },
                error: function (e) {
                    console.log("error", e);
                    alert("请求失败，请重试");
                }
            });
        },
        reload:function () {
            vm.showList = true;
            vm.showAdd = false;
            $("#jqGrid").jqGrid().trigger("reloadGrid");
        }
    }
});